package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.cheyunkeji.er.view.evaluate.BCSMCheckItemView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class DriveCabinCheckBCSM extends BaseActivity {

  private static final String TAG = DriveCabinCheckBCSM.class.getSimpleName();

  @BindView(R.id.v_topbar)
  TopBar vTopbar;

  @BindView(R.id.bcsm_85)
  BCSMCheckItemView bcsm85;

  @BindView(R.id.bcsm_86)
  BCSMCheckItemView bcsm86;

  @BindView(R.id.bcsm_87)
  BCSMCheckItemView bcsm87;

  @BindView(R.id.bcsm_88)
  BCSMCheckItemView bcsm88;

  @BindView(R.id.bcsm_89)
  BCSMCheckItemView bcsm89;

  @BindView(R.id.bcsm_90)
  BCSMCheckItemView bcsm90;

  @BindView(R.id.bcsm_91)
  BCSMCheckItemView bcsm91;

  @BindView(R.id.bcsm_92)
  BCSMCheckItemView bcsm92;

  @BindView(R.id.bcsm_93)
  BCSMCheckItemView bcsm93;

  @BindView(R.id.bcsm_94)
  BCSMCheckItemView bcsm94;

  @BindView(R.id.bcsm_95)
  BCSMCheckItemView bcsm95;

  @BindView(R.id.bcsm_96)
  BCSMCheckItemView bcsm96;

  @BindView(R.id.bcsm_97)
  BCSMCheckItemView bcsm97;

  @BindView(R.id.bcsm_98)
  BCSMCheckItemView bcsm98;

  @BindView(R.id.bcsm_99)
  BCSMCheckItemView bcsm99;

  @BindView(R.id.bcsm_100)
  BCSMCheckItemView bcsm100;

  @BindView(R.id.bcsm_101)
  BCSMCheckItemView bcsm101;

  @BindView(R.id.bcsm_102)
  BCSMCheckItemView bcsm102;

  @BindView(R.id.bcsm_103)
  BCSMCheckItemView bcsm103;

  @BindView(R.id.bcsm_104)
  BCSMCheckItemView bcsm104;

  @BindView(R.id.bcsm_105)
  BCSMCheckItemView bcsm105;

  @BindView(R.id.bcsm_106)
  BCSMCheckItemView bcsm106;

  @BindView(R.id.bcsm_107)
  BCSMCheckItemView bcsm107;

  @BindView(R.id.bcsm_108)
  BCSMCheckItemView bcsm108;

  @BindView(R.id.bcsm_109)
  BCSMCheckItemView bcsm109;

  @BindView(R.id.bcsm_110)
  BCSMCheckItemView bcsm110;

  @BindView(R.id.bcsm_111)
  BCSMCheckItemView bcsm111;

  @BindView(R.id.bcsm_112)
  BCSMCheckItemView bcsm112;

  @BindView(R.id.bcsm_113)
  BCSMCheckItemView bcsm113;

  @BindView(R.id.bcsm_114)
  BCSMCheckItemView bcsm114;

  @BindView(R.id.bcsm_115)
  BCSMCheckItemView bcsm115;

  @BindView(R.id.bcsm_116)
  BCSMCheckItemView bcsm116;

  @BindView(R.id.bcsm_117)
  BCSMCheckItemView bcsm117;

  @BindView(R.id.bcsm_118)
  BCSMCheckItemView bcsm118;

  @BindView(R.id.activity_drive_cabin_check_bcsm)
  LinearLayout activityDriveCabinCheckBcsm;

  private Map<Integer, BCSMCheckItemView> itemViewList = null;

  private List<EvaluateDetailResult2.DetectionBean.CockpitMarkBean> cockpitBeanList = null;

  @Override
  protected void init() {

    setContentView(R.layout.er_activity_drive_cabin_check_bcsm);
    ButterKnife.bind(this);

    itemViewList = new HashMap<>();
    itemViewList.put(85, bcsm85);
    itemViewList.put(86, bcsm86);
    itemViewList.put(87, bcsm87);
    itemViewList.put(88, bcsm88);
    itemViewList.put(89, bcsm89);
    itemViewList.put(90, bcsm90);
    itemViewList.put(91, bcsm91);
    itemViewList.put(92, bcsm92);
    itemViewList.put(93, bcsm93);
    itemViewList.put(94, bcsm94);
    itemViewList.put(95, bcsm95);
    itemViewList.put(96, bcsm96);
    itemViewList.put(97, bcsm97);
    itemViewList.put(98, bcsm98);
    itemViewList.put(99, bcsm99);
    itemViewList.put(100, bcsm100);
    itemViewList.put(101, bcsm101);
    itemViewList.put(102, bcsm102);
    itemViewList.put(103, bcsm103);
    itemViewList.put(104, bcsm104);
    itemViewList.put(105, bcsm105);
    itemViewList.put(106, bcsm106);
    itemViewList.put(107, bcsm107);
    itemViewList.put(108, bcsm108);
    itemViewList.put(109, bcsm109);
    itemViewList.put(110, bcsm110);
    itemViewList.put(111, bcsm111);
    itemViewList.put(112, bcsm112);
    itemViewList.put(113, bcsm113);
    itemViewList.put(114, bcsm114);
    itemViewList.put(115, bcsm115);
    itemViewList.put(116, bcsm116);
    itemViewList.put(117, bcsm117);
    itemViewList.put(118, bcsm118);
  }

  @Override
  protected void initView() {

    vTopbar.setLeftBack();
    vTopbar.setRight("保存", -1, this);
    vTopbar.setTitle("驾驶舱补充说明");

    if (null != MyApplication.getInstance().getEvaluateResult().getDetection()
        && null != MyApplication.getInstance().getEvaluateResult().getDetection().getCockpit_mark()) {

      cockpitBeanList = MyApplication.getInstance().getEvaluateResult().getDetection().getCockpit_mark();

      if (cockpitBeanList.size() > 0) {

        displayInfo();
      }
    }
  }

  private void displayInfo() {

    for (EvaluateDetailResult2.DetectionBean.CockpitMarkBean bean : cockpitBeanList) {

      itemViewList.get(bean.getId()).setCheckedIndex(bean.getStatus() - 1);
      itemViewList.get(bean.getId()).setTag(bean);
    }
  }

  @Override
  protected void initData() {}

  @Override
  public void onClick(View v) {
    super.onClick(v);

    if (cockpitBeanList == null) {
      cockpitBeanList = new ArrayList<>();
    } else {
      cockpitBeanList.clear();
    }

    if (v.getId() == R.id.vRight) {

      for (int index : itemViewList.keySet()) {
        if (3 != itemViewList.get(index).getCurrentCheckedIndex()) {

          cockpitBeanList.add(
              new EvaluateDetailResult2.DetectionBean.CockpitMarkBean(
                  index, itemViewList.get(index).getCurrentCheckedIndex() + 1));
        }
      }

      String myData =
          new StringBuilder("{\"cockpit_mark\":")
              .append(new Gson().toJson(cockpitBeanList))
              .append("}")
              .toString();

      Log.e(TAG, "onClick: " + new Gson().toJson(cockpitBeanList));

      HashMap<String, String> params = new HashMap<>();
      //            params.put("authkey",
      // UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
      //            params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(),
      // ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
      params.put("aid", MyApplication.getInstance().getEvaluateResult().getAid());

      params.put("detection", myData);

      ApiClient.postForm(
          HttpConstants.INSERT_ARCHIVE_INFO,
          params,
          new RespCallback<EvaluateDetailResult2>() {

            @Override
            protected void onRequestFailed(String msg) {
              SToast.show(msg);
            }

            @Override
            protected void onSuccess(EvaluateDetailResult2 data) {
              if (null != data) {
                MyApplication.getInstance().setEvaluateResult(data);
                SToast.show("保存成功");
                MyApplication.getInstance().removeActivity(DriveCabinCheckBCSM.class);
                finish();
              }
            }

            @Override
            public void onBefore(Request request, int id) {
              showDialog();
            }

            @Override
            public void onAfter(int id) {
              dismissDialog();
            }
          });
    }
  }
}
